package com.hemaapp.yjnh.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.chat.ChatFresh;
import com.hemaapp.hm_FrameWork.chat.FirPagCount;
import com.hemaapp.hm_FrameWork.chat.FirPagDBClient;
import com.hemaapp.hm_FrameWork.chat.HemaChat;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseFragment;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.ViewPagerFragment;
import com.hemaapp.yjnh.adapter.ChatListAdapter;

import java.util.ArrayList;

/**
 * 类描述：聊天消息列表
 * 创建人：GuiyouZhang
 * 创建时间：2016/8/2 20:05
 * 修改备注：
 */
public class ChatListFragment extends ViewPagerFragment implements ChatFresh.ChatFreshListener{

    private RefreshLoadmoreLayout parentLayout;
    private RecyclerView recyclerView;

    private FirPagDBClient firPagDBClient;
    private ArrayList<FirPagCount> counts = new ArrayList<FirPagCount>();//聊天消息记录
    private ChatListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.fragment_viewpager_refresh_recycler);
        super.onCreate(savedInstanceState);
    }

    public ArrayList<FirPagCount> getCounts() {
        return counts;
    }

    @Override
    public void onStop() {
        super.onStop();
        ChatFresh.removeChatFreshListener(this);
    }

    @Override
    public void onDestroy() {
        ChatFresh.removeChatFreshListener(this);
        if (firPagDBClient != null)
            firPagDBClient.close();
        super.onDestroy();
        HemaChat chat = HemaChat.getInstance(getActivity());
        chat.release();
    }

    @Override
    public void onResume() {
        //刷新聊天记录
        refreshChatData();
        ChatFresh.addChatFreshListener(this);
        super.onResume();
    }

    private void refreshChatData() {
        if (firPagDBClient == null)
            firPagDBClient = FirPagDBClient.get(getActivity());
        ArrayList<FirPagCount> counts = firPagDBClient.select();
        this.counts.clear();
        if (counts != null)
            this.counts.addAll(counts);
        refreshView();
    }

    private void refreshView() {
        if (firPagDBClient == null)
            firPagDBClient = FirPagDBClient.get(getActivity());
        ArrayList<FirPagCount> counts = firPagDBClient.select();
        this.counts.clear();
        if (counts != null)
            this.counts.addAll(counts);
        if (adapter == null) {
            adapter = new ChatListAdapter(getActivity(), this.counts);
            recyclerView.setAdapter(adapter);
        }else
            adapter.notifyDataSetChanged();
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask hemaNetTask) {

    }

    @Override
    protected void callAfterDataBack(HemaNetTask hemaNetTask) {

    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask hemaNetTask, HemaBaseResult hemaBaseResult) {

    }

    @Override
    protected void callBackForServerFailed(HemaNetTask hemaNetTask, HemaBaseResult hemaBaseResult) {

    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask hemaNetTask, int i) {

    }

    @Override
    protected void findView() {
        parentLayout = (RefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    }

    @Override
    protected void setListener() {
//        parentLayout.setOnStartListener(new XtomRefreshLoadmoreLayout.OnStartListener() {
//            @Override
//            public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
//            }
//
//            @Override
//            public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
//            }
//        });
        parentLayout.setRefreshable(false);
        parentLayout.setLoadmoreable(false);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
    }

    @Override
    public void chatFresh() {
        refreshView();
    }

    public void clearAll() {
        if (firPagDBClient == null)
            firPagDBClient = FirPagDBClient.get(getActivity());
        firPagDBClient.clearTalk();
        refreshView();
    }

    public void readAll() {
        if (firPagDBClient == null)
            firPagDBClient = FirPagDBClient.get(getActivity());
        firPagDBClient.updateCount0();
        refreshView();
    }

    @Override
    public void fetchData() {

    }
}
